#include <iostream>
#include <cassert>
#include <vector>
#include <algorithm>
#include <sstream>
#include <list>
#include <stack>
#include <queue>
#include <map>
using namespace std;

struct ListNode {
     int val;
     ListNode *next;
     ListNode() : val(0), next(nullptr) {}
     ListNode(int x) : val(x), next(nullptr) {}
     ListNode(int x, ListNode *next) : val(x), next(next) {}
};
//打印链表
void displayList(ListNode *p){
    while( p ){
        cout<< p->val ;
        p = p->next;
        if(p) cout<<"->";
    }
    cout<<endl;
}

//解决方案类
class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        if(intervals.size() < 2) return intervals;
        vector<vector<int>> rt;
        //auto msortFunc = [](vector<int>a,vector<int>b) {return a[0] < b[0];}; //自定义排序比较法则
        sort(intervals.begin(),intervals.end()) ; //排序
        rt.push_back(intervals[0]);
        for(auto &it : intervals) {
            auto &it2 = rt.back(); //上一个有效区间
            int a = it2[0] ,  b = it2[1] ,  c = it[0] , d = it[1] ;//区间值
            if( a<=c && c<=b &&d>= b ) it2[1] = d;
            else if(b<c) rt.push_back({c,d});
        }
        return rt;
    }
};
int main(){
    Solution sp;
    vector<vector<int>> in = {
                            {1,4},
                            {4,7},
                            {8,10}};
    for(auto i : in ) cout<<"["<<i[0]<<","<<i[1]<<"] ";cout<<endl;
    vector<vector<int>> ans;
    ans = sp.merge(in);
    for(auto i : ans ) cout<<"["<<i[0]<<","<<i[1]<<"] ";cout<<endl;
    return 0;
}